package com.rent.view;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.SocketTimeoutException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import org.apache.log4j.Logger;

import com.rent.Impl.BrandDaoImpl;
import com.rent.Impl.CarDaoImpl;
import com.rent.Impl.RentDaoImpl;
import com.rent.Impl.TypeDaoImpl;
import com.rent.Impl.UserDaoImpl;
import com.rent.Impl.VipDaoImpl;
import com.rent.bean.Brand;
import com.rent.bean.Car;
import com.rent.bean.History;
import com.rent.bean.Rent;
import com.rent.bean.Type;
import com.rent.dao.BrandDao;
import com.rent.dao.CarDao;
import com.rent.dao.RentDao;
import com.rent.dao.TypeDao;
import com.rent.dao.UserDao;
import com.rent.dao.VipDao;
import com.rent.utils.ViewUtils;

/**
 * 管理员选项
 * @author xin
 *
 */
public class AdminOption {
	/**
	 * 用户信息接口
	 */
	UserDao userDao = new UserDaoImpl();
	/**
	 * 车辆信息
	 */
	CarDao carDao = new CarDaoImpl();
	/**
	 * 订单信息
	 */
	RentDao rentDao = new RentDaoImpl();
	/**
	 * 会员用户信息
	 */
	VipDao vipDao = new VipDaoImpl();
	/**
	 * 品牌
	 */
	BrandDao brandDao = new BrandDaoImpl();
	/**
	 * 类型
	 */
	TypeDao typeDao = new TypeDaoImpl();
	/**
	 * 视图工具
	 */
	ViewUtils v = new ViewUtils();
	Scanner sn = new Scanner(System.in);
	private Logger log = Logger.getLogger(AdminOption.class);
	//调用日期格式化对象
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 查看所有用户
	 */
	public void showUser() { 
		v.printTitle("用户信息");
		v.show_user_rent(userDao.sele_rent());
		System.out.println("1.查看用户的租赁记录 ");
		switch (sn.nextInt()) {
		case 1:
			showRentByUname();
			break;
		}
	}
	
	public void showRentByUname() {
		System.out.println("请输入用户名：");
		String uname = sn.next();
		List<Rent> list = rentDao.seleByUname(uname);
		if(list.isEmpty()) {
			System.out.println("此用户没有租赁信息");
			return;
		}
		v.show_rent(list);
		System.out.println("1.查看详细信息 ");
		switch (sn.nextInt()) {
		case 1:
			rentDetailed();
			break;
		}
	}
	/**
	 * 按状态查询
	 */
	private void showByStatus() {
		System.out.println("状态： 1: 待支付 2:支付完成 3: 租赁中 4: 已完成 5:已取消");
		System.out.println("请输入：");
		List<Rent> r = rentDao.seleByStatus(sn.nextInt());
		v.show_rent(r);
	}

	/**
	 * 按时间查询
	 */
	private void showByTime() {
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		System.out.print("开始时间：");
		String startTime = sn.next();
		try {
			s.parse(startTime);
		} catch (ParseException e) {
			System.out.println("输入日期格式不正确");
			e.printStackTrace();
		}
		System.out.print("结束时间：");	
		String endTime = sn.next();
		try {
			s.parse(endTime);
		} catch (ParseException e) {
			System.out.println("输入日期格式不正确");
			e.printStackTrace();
		}
		List<Rent> r = rentDao.seleByTime(startTime,endTime);
		v.show_rent(r);
	}

	/**
	 * 查看车辆
	 */
	public void showCar() {
		v.printTitle("汽车信息");
		Car c = new Car();
		List<Car> list = carDao.sele_car(3, c);
		v.show_car_admin(list);
		System.out.println("1.添加车辆 2.修改车辆 ");
		switch (sn.nextInt()) {
		case 1:
			addCar();
			break;
		case 2:
			update();
			break;
		default:
			System.out.println("输入不合法");
			break;
		}
	}
	/**
	 * 修改车辆
	 */
	private void update() {
		Car c = new Car();
		Brand brand = null;
		Type type = null;
		c.setIsdelete(-1);
		System.out.println("请输入修改车辆新编号：");
		c.setCarid(sn.nextInt());
		Car seleById = carDao.seleById(c.getCarid());
		if(seleById==null) {
			System.out.println("没有此车辆！");
			return;
		}
		v.show_brand(brandDao.seleByBrand(null));
		System.out.println("请选择品牌:");
		c.setBid(sn.nextInt());
		brand = brandDao.seleById(c.getBid());
		if(brand==null) {
			System.out.println("没有这个品牌");
			return;
		}
		Type t = new Type();
		t.setBid(c.getBid());
		List<Type> types = typeDao.seleByType(t);
		if(types.isEmpty()) {
			System.out.println("这个品牌没有型号");
			return;
		}
		v.show_type(types);
		System.out.println("请选择型号：");
		c.setTid(sn.nextInt());
		type = typeDao.seleById(c.getTid());
		c.setCprice(type.getPrice());
		System.out.println("颜色：");
		c.setColor(sn.next());
		System.out.println("变速箱：");
		c.setGearbox(sn.next());
		System.out.println("座位数： ");
		c.setSeat(sn.nextInt());
		System.out.println("车牌号：");
		try {
			c.setCarNumber(sn.next());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("(0:可用 1:不可用)是否删除：");
		c.setIsdelete(sn.nextInt());
		System.out.println("(1.普通车辆 2.会员车辆)等级：");
		c.setCgrade(sn.nextInt());
		System.out.println("(1:车辆空闲，可租；2:车辆租出；3：车辆报修;4:车辆在修;5:已报废移除)状态：");
		c.setCstatus(sn.nextInt());
		System.out.println("发动机编号：");
		c.setEngineNumber(sn.next());
		System.out.println("油箱标定容积：");
		c.setGasolineVolume(sn.nextInt());
		System.out.println("实际装油：");
		c.setGasolineActualVolume(sn.nextInt());
		System.out.println("汽油编号：");
		c.setGasolineNumber(sn.nextInt());
		try {
			carDao.updateCar(c);
		} catch (Exception e) {
			log.debug(e);
			System.out.println("系统有点小问题");
		}
	}
	
	/**
	 * 添加车辆
	 */
	public void addCar() {
		Car c = new Car();
		v.show_brand(brandDao.seleByBrand(null));
		System.out.println("请选择汽车品牌：");
		c.setBid(sn.nextInt());
		Brand brand = brandDao.seleById(c.getBid());
		if(brand==null) {
			System.out.println("没有这个品牌");
			return;
		}
		Type t = new Type();
		t.setBid(c.getBid());
		List<Type> types = typeDao.seleByType(t);
		if(types.isEmpty()) {
			System.out.println("此品牌没有型号");
			return;
		}
		v.show_type(types);
		System.out.println("请选择汽车的型号：");
		c.setTid(sn.nextInt());
		Type type = typeDao.seleById(c.getTid());
		if(type==null) {
			System.out.println("没有这个型号");
			return;
		}
		System.out.println("汽车的颜色：");
		c.setColor(sn.next());
		System.out.println("汽车的座位数：");
		c.setSeat(sn.nextInt());
		System.out.println("汽车的变速箱(手动挡/自动挡)：");
		c.setGearbox(sn.next());
		System.out.println("发动机编号：");
		c.setEngineNumber(sn.next());
		System.out.println("油箱容量：");
		c.setGasolineVolume(sn.nextInt());
		System.out.println("实际油箱容量：");
		c.setGasolineActualVolume(sn.nextInt());
		System.out.println("汽车加什么型号汽油：");
		c.setGasolineNumber(sn.nextInt());
		System.out.println("车牌号：");
		try {
			c.setCarNumber(sn.next());
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		System.out.println("是否可用(y/n)：");
		switch (sn.next()) {
		case "y":
			c.setIsdelete(0);
			break;
		case "n":
			c.setIsdelete(1);
			break;
		default:
			System.out.println("输入不合法");
			return;
		}
		c.setCstatus(1);
		try {
			carDao.add(c);
			System.out.println("添加成功");
		}catch (Exception e) {
			System.out.println("添加失败");
			log.debug(e);
		}
	}
	
	/**
	 * 查看订单
	 */
	public void showRent() {
		System.out.println("租赁信息");
		List<Rent> seleAll = rentDao.seleAll();
		v.show_rent(seleAll);
		if(seleAll.isEmpty()) {
			return;
		}
		System.out.println("1.查看详细信息  2.按时间查询 3.按状态查询");
		switch (sn.nextInt()) {
		case 1:
			rentDetailed();
			break;
		case 2:
			showByTime();
			break;
		case 3:
			showByStatus();
			break;
		}
	}
	//查看详细租赁信息
	public void rentDetailed() {
		System.out.println("请输入编号：");
		Rent rent = rentDao.seleById(sn.nextInt());
		if(rent==null) {
			System.out.println("没有此租赁信息");
			return;
		}
		Car car = carDao.seleById(rent.getCid());
		v.show_rent(rent, car);
		
	}
	/**
	 * 退还汽车押金
	 */
	public void returnDeposit() {
		System.out.println("请输入租赁单号：");
		Rent rent = rentDao.seleById(sn.nextInt());
		if(rent==null) {
			System.out.println("没有此租赁信息");
			return;
		}
		v.show_rent(rent);
		System.out.println("1.退还汽车押金 2.退还违章押金");
		switch (sn.nextInt()) {
		case 1:
			//汽车押金
			if(rent.getCar_deposit_status()==1) {
				System.out.println("您的汽车押金已经退还了");
				return;
			}
			System.out.println("是否扣除金额：(y/n)");
			switch (sn.next()) {
			case "y":
				System.out.println("扣除多少金额:");
				double money = sn.nextDouble();
				if(money>rent.getCar_deposit()) {
					System.out.println("输入金额不正确！");
					return;
				}
				rent.setCar_deposit(rent.getCar_deposit()-money);
				break;
			}
			//退还押金
			rentDao.updateCarDeposit(rent);
			//添加描述
			System.out.println("输入汽车押金的描述(y/n)：");
			switch (sn.next()) {
			case "y":
				System.out.println("输入描述：");
				rent.setCar_deposit_describe(sn.next());
				rentDao.updateCarDepositDescribe(rent);
				break;
			}
			break;	
		case 2:
			//违章押金
			if(rent.getViolation_deposit_status()==1) {
				System.out.println("您的汽车押金已经退还了");
				return;
			}
			System.out.println("是否扣除金额：(y/n)");
			switch (sn.next()) {
			case "y":
				System.out.println("扣除多少金额:");
				double money = sn.nextDouble();
				if(money>rent.getViolation_deposit()) {
					System.out.println("输入金额不正确！");
					return;
				}
				rent.setViolation_deposit(rent.getViolation_deposit()-money);
				break;
			}
			//退还押金
			rentDao.updateViolationDeposit(rent);
			//添加描述
			System.out.println("输入违章押金的描述(y/n)：");
			switch (sn.next()) {
			case "y":
				System.out.println("输入描述：");
				rent.setViolation_deposit_describe(sn.next());
				rentDao.updateViolationDepositDescribe(rent);
				break;
			}
			break;	
		}
	}
	/**
	 * 损坏车辆
	 */
	public void showCarDamage() {
		Car c = new Car();
		c.setCstatus(3);
		List<Car> sele_car = carDao.sele_car(3,c);
		v.show_car(sele_car);
		if(!sele_car.isEmpty()) {
			return;
		}
		System.out.println("1.导出损坏车辆 ");
		switch (sn.nextInt()) {
		case 1:
			carExport(sele_car);
			break;
		default:
			break;
		}
	}
	/**
	 * 导出
	 * @param cars
	 */
	public void carExport(List<Car> cars) {
		Writer w = null;
		try {
			w = new FileWriter("damage.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
		BufferedWriter bw = new BufferedWriter(w);
		try {
			bw.write("\t汽车损坏信息");
			bw.newLine();
			//编号 品牌 型号 颜色 座位数 车牌号 
			bw.write("编号\t品牌\t价格\t车型\t颜色\t变速箱\t座位数\t车牌号\t发动机编号\t标定容积（升）\t实际装油（升）\t汽油编号");
			bw.newLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		for(Car c : cars) {
			try {
				bw.write(c.getCarid()+"\t"+
						c.getCbrand()+"\t"+
						c.getCprice()+"\t"+
						c.getCtype()+"\t"+
						c.getColor()+"\t"+
						c.getGearbox()+"\t"+
						c.getSeat()+"\t"+
						c.getCarNumber()+"\t"+
						c.getEngineNumber()+"\t"+
						c.getGasolineVolume()+"\t"+
						c.getGasolineActualVolume()+"\t"+
						c.getGasolineNumber());
				bw.newLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("打印成功");
		}
		try {
			bw.close();
			w.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 统计
	 */
	public void statistics() {
		int toDay = rentDao.seleToDay();
		int toMonth = rentDao.seleToMonth();
		double sumMoney = rentDao.seleSumMoney();
		double toMonthMoney = rentDao.seleToMonthMoney();
		double toDayMoney = rentDao.seleToDayMoney();
		List<Car> cars = carDao.seleRank();
		int vipCount = vipDao.seleCount();
		System.out.println("总计收入："+sumMoney+"￥");
		System.out.println("本月收入："+toMonthMoney+"￥");
		System.out.println("今天收入："+toDayMoney+"￥");
		System.out.println("共有 "+vipCount+" 个会员");
		System.out.println("今天 "+toDay+" 个订单");
		System.out.println("这个月 "+toMonth+" 个订单");
		System.out.println("本月最受欢迎的车辆:");
		for(int i = 0;i<cars.size();i++) {
			Car c = cars.get(i);
			System.out.println("No."+(i+1)+"\t"+c.getCbrand()+"\t"+c.getCtype()+"\t租赁次数："+c.num);			
		}
	}
	/**
	 * 查询会员
	 */
	public void showVip() {
		v.show_user_vip(vipDao.seleVip());
	}
	/**
	 * 查询到期会员
	 */
	public void showVipExpire() {
		v.show_user_vip(vipDao.seleVipExpire());
	}
	/**
	 * 汽车品牌
	 */
	public void showBrand() {
		Brand b = new Brand();
		v.show_brand(brandDao.seleByBrand(b));
		System.out.println("1.添加 2.修改");
		switch (sn.nextInt()) {
		case 1:
			addBrand();
			break;
		case 2:
			updateBrand();
			break;
		}
	}
	
	public void addBrand() {
		Brand b = new Brand();
		System.out.println("请输入品牌名称：");
		b.setBrand(sn.next());
		try {
			brandDao.add(b);
		}catch (Exception e) {
			System.out.println("添加失败");
			e.printStackTrace();
		}
	}
	
	public void updateBrand() {
		Brand b = new Brand();
		System.out.println("请输入要修改的品牌编号：");
		b.setBid(sn.nextInt());
		System.out.println("请输入要修改的品牌名称：");
		b.setBrand(sn.next());
		try {
			brandDao.update(b);
		}catch (Exception e) {
			System.out.println("修改失败");
			e.printStackTrace();
		}
	}
	
	/**
	 * 汽车类型
	 */
	public void showType() {
		Type t = new Type();
		v.show_type(typeDao.seleByType(t));
		System.out.println("1.添加 2.修改");
		switch (sn.nextInt()) {
		case 1:
			addType();
			break;
		case 2:
			updateType();
			break;
		}
	}

	private void updateType() {
		Brand b = new Brand();
		Type t = new Type();
		System.out.println("请输入要修改的编号：");
		t.setTid(sn.nextInt());
		List<Type> types = typeDao.seleByType(t);
		if(types.isEmpty()) {
			System.out.println("没有这个型号");
			return;
		}
		v.show_brand(brandDao.seleByBrand(b));
		System.out.println("请选择汽车品牌：");
		b.setBid(sn.nextInt());
		List<Brand> brands = brandDao.seleByBrand(b);
		if(brands.isEmpty()) {
			System.out.println("没有这个品牌");
			return;
		}
		Brand brand = brands.get(0);
		t.setBid(brand.getBid());
		System.out.println("请输入型号：");
		t.setType(sn.next());
		System.out.println("请输入价格：");
		t.setPrice(sn.nextDouble());
		try {
			typeDao.update(t);
		}catch (Exception e) {
			System.out.println("添加失败");
			e.printStackTrace();
		}
	}
	private void addType() {
		Brand b = new Brand();
		Type t = new Type();
		v.show_brand(brandDao.seleByBrand(b));
		System.out.println("请选择汽车品牌：");
		b.setBid(sn.nextInt());
		List<Brand> brands = brandDao.seleByBrand(b);
		v.show_brand(brands);
		if(brands.isEmpty()) {
			System.out.println("没有这个品牌");
			return;
		}
		Brand brand = brands.get(0);
		t.setBid(brand.getBid());
		System.out.println("请输入型号：");
		t.setType(sn.next());
		System.out.println("请输入价格：");
		t.setPrice(sn.nextDouble());
		try {
			typeDao.add(t);
		}catch (Exception e) {
			System.out.println("添加失败");
			e.printStackTrace();
		}
	}
}
